/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action;

import controller.Action;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Paciente;
import persistence.PacienteDAO;
import util.Validador;


/**
 *
 * @author aurelio.costa
 */
public class GravarPacienteAction implements Action{

    @Override
    public void execute(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        String nome = request.getParameter("textNome");
        String data = request.getParameter("textDtNascimento");
        String profissao = request.getParameter("textProfissao");
        
        if(!Validador.validaString(nome, data, profissao)){            
            response.sendRedirect("index.jsp");
        }else {
            try {
                Paciente paciente = new Paciente();
                paciente.setNome(nome);
                paciente.setDtNascimento(new Date(data));
                paciente.setProfissao(profissao);
                PacienteDAO.getInstace().save(paciente);
                response.sendRedirect("sucesso.jsp");
                
            } catch (SQLException e) {
                response.sendRedirect("erro.jsp");
                e.printStackTrace();
            
            } catch (ClassNotFoundException e) {
                response.sendRedirect("contatoErro.jsp");
                e.printStackTrace();
            }
        }
    }
    
}
